package com.chaodows.commons.exception;

import com.chaodows.commons.dto.result.ResultCode;
import java.util.HashMap;
import java.util.Map;

/**
 * ============================
 *
 * @Author pengzhichao
 * @Description //TUDO $
 * @Date 2021/7/26 18:50
 * @Version 1.0
 * ============================
 **/
public class BusinessException extends RuntimeException {

    private static final long serialVersionUID = -7161934106638480046L;
    protected static Map<Integer, String> mapMessage = new HashMap<Integer, String>();

    public static final int SYSTEM_INTERNAL_ERROR = 10001;
    public static final int PARAMETER_ERROR = 10002;
    public static final int RECORD_DUPLICATED = 11001;
    public static final int RECORD_NOTEXIST = 11002;

    static {
        mapMessage.put(SYSTEM_INTERNAL_ERROR, "系统内部错误");
        mapMessage.put(PARAMETER_ERROR, "业务参数异常");
        mapMessage.put(RECORD_DUPLICATED, "记录重复");
        mapMessage.put(RECORD_NOTEXIST, "记录不存在");
    }

    private ResultCode resultCode;
    private MessageException data;

    public BusinessException(Throwable e) {
        super(e);
        this.data = MessageException.getInstance(e.getMessage());
    }

    public BusinessException(String msg, Throwable e) {
        super(e);
        this.data = MessageException.getInstance(msg);
    }

    public BusinessException(ResultCode resultCode) {
        super(resultCode.message());
        this.resultCode = resultCode;
    }

    public BusinessException(String title, String message) {
        super(message);
        this.data = MessageException.getInstance(title, message);
        resultCode = ResultCode.SERVICE_INNER_ERROR;
    }

    public BusinessException(String message) {
        super(message);
        this.data = MessageException.getInstance(message);
    }

    public ResultCode getResultCode() {
        return resultCode;
    }

    public void setResultCode(ResultCode resultCode) {
        this.resultCode = resultCode;
    }

    public MessageException getData() {
        return data;
    }

    public void setData(MessageException data) {
        this.data = data;
    }
}
